// pages/order/order.js
Page({

    data:{

        orderId:"",
        token:"",
        status:"",
        isDeliveryOrder:false,
        isConsumer:false,
        isOrderDone:false,
        isOrderTaken:false,

		deliveryOrderInfo: {

        },

        taskOrderInfo: {

        },

        counterInfo:{

        }
        

    },


    //跳转至聊天页面并传递聊天对象的openid
    navigaToChatRoom: function(e){
        wx.navigateTo({
          url: '/pages/chatRoom/chatRoom?openid='+JSON.stringify(this.data.counterInfo.openid)
        })          
    },


    //取消订单函数
    cancelBut: function(e){
        var that=this
        var pages = getCurrentPages()
        var beforePage = pages[pages.length - 2];
        wx.showModal({
          title:"取消订单",
          content:"是否确定取消订单",
          success(res){
              if(res.confirm&&that.data.isDeliveryOrder){ 
                wx.request({
                  url: 'http://119.23.252.15:10002/delivery/cancelOrder',
                  method:"POST",
                  data:{
                      token:wx.getStorageSync('token'),
                      orderId:that.data.orderId
                  },
                  success(res){
                    if(res.data.status==1){
                    wx.navigateBack({
                      delta: 1,
                      success:function(){
                        beforePage.onLoad()
                        wx.showToast({
                          title: '成功取消订单',
                        })
                      }
                    })
                  }
                  else{
                    wx.showToast({
                      title: '取消订单失败 似乎遇到了什么BUG了',
                    })
                  }
                  }
                })

              }

              else if (res.confirm&&!(that.data.isDeliveryOrder)){
                wx.request({
                  url: 'http://119.23.252.15:10002/task/cancelOrder',
                  method:"POST",
                  data:{
                    token:wx.getStorageSync('token'),
                    orderId:that.data.orderId
                },
                success(res){
                  if(res.data.status==1){
                  wx.navigateBack({
                    delta: 1,
                    success:function(){
                      beforePage.onLoad()
                      wx.showToast({
                        title: '成功取消订单',
                      })
                    }
                  })
                }
                else{
                  wx.showToast({
                    title: '取消订单失败 似乎遇到了什么BUG了',
                  })
                }
                }
                })

              }

              else if (res.cancel)
              {
                  console.log('取消')
              }
          },
          
        })

    },


    //完成订单函数
    doneBut: function(e){
        var that=this
        var pages = getCurrentPages()
        var beforePage = pages[pages.length - 2];
        wx.showModal({
          title:"完成订单", 
          content:"是否确定完成订单",
          success(res){
              if(res.confirm&&that.data.isDeliveryOrder){  
                wx.request({
                  url: 'http://119.23.252.15:10002/delivery/completeOrder',
                  method:"POST",
                  data:{
                      token:wx.getStorageSync('token'),
                      orderId:that.data.orderId
                  },
                  success(res){
                    if(res.data.status==1){
                    wx.navigateBack({
                      delta: 1,
                      success:function(){
                        beforePage.onLoad()
                        wx.showToast({
                          title: '成功完成订单',
                        })
                      }
                    })
                  }
                  else{
                    wx.showToast({
                      title: '完成订单失败 似乎遇到什么BUG了',
                    })
                  }
                  }
                })

              }

              else if (res.confirm&&!(that.data.isDeliveryOrder)){
                wx.request({
                  url: 'http://119.23.252.15:10002/task/completeOrder',
                  method:"POST",
                  data:{
                    token:wx.getStorageSync('token'),
                    orderId:that.data.orderId
                },
                success(res){
                  if(res.data.status==1){
                  wx.navigateBack({
                    delta: 1,
                    success:function(){
                      beforePage.onLoad()
                      wx.showToast({
                        title: '成功完成订单',
                      })
                    }
                  })
                }
                else{
                  wx.showToast({
                    title: '完成订单失败 似乎遇到什么BUG了',
                  })
                }
                }
                })
              }

              else if (res.cancel)
              {
                  console.log('取消')
              }
          },
          
        })

    },




    //初始化接受orderId并请求订单详细信息
    onLoad: function(option){

      console.log(option.type)

      var that = this

        if(option.type=="delivery")
        {
          console.log("是快递订单")
        that.setData({
            orderId:JSON.parse(option.current),
            isDeliveryOrder:true,
        })

        wx.request({
          url: 'http://119.23.252.15:10002/delivery/getOrderInfo',
          method:"POST",
          data:{
              token:wx.getStorageSync('token'),
              orderId:JSON.parse(option.current)
          },
          success(res){
            console.log(res.data.data.orderInfo.status)
            that.setData({
              counterInfo:res.data.data.counterInfo,
              deliveryOrderInfo:res.data.data.orderInfo,
              status:res.data.data.orderInfo.status
            })


              if (res.data.data.myIdentity!=="servant"){
                  that.setData({
                      isConsumer:true
                  })
                  console.log("设置身份为顾客")
              }

              if (res.data.data.orderInfo.status=="订单完成等待顾客付款"||res.data.data.orderInfo.status=="订单取消")
              {
                  that.setData({
                      isOrderDone:true
                  })
              }

              if(res.data.data.orderInfo.status=="订单未被接单")
              {
                  that.setData({
                      isOrderTaken:true
                  })
              }
 

          }

        })
      }



      if(option.type=="task")
      {
        console.log("是跑腿订单")
            that.setData({
                isDeliveryOrder:false,
                orderId: JSON.parse(option.current),
            })

            wx.request({
                url: 'http://119.23.252.15:10002/task/getOrderInfo',
                method:"POST",
                data:{
                    token:wx.getStorageSync('token'),
                    orderId:that.data.orderId
                },
                success(res){
                    console.log(res.data)  

                    if (res.data.data.myIdentity!=="servant"){
                        that.setData({
                            isConsumer:true
                        })
                    }
      

                    if (res.data.data.orderInfo.status=="订单完成等待顾客付款"||res.data.data.orderInfo.status=="订单取消")
                    {
                        that.setData({
                            isOrderDone:true
                        })
                    }

                    if(res.data.data.orderInfo.status=="订单未被接单")
                    {
                        that.setData({
                            isOrderTaken:true
                        })
                    }


      
                    that.setData({
                      status:res.data.data.orderInfo.status,
                      counterInfo:res.data.data.counterInfo,
                      taskOrderInfo:res.data.data.orderInfo
                    })
                }
      
              })

        }

        }
})